<template>
	<view>
		<u-navbar title="新增收货地址"></u-navbar>

		<view class="form_box">
			<form @submit="formSubmit">
				<view class="formCon">
					<view class="formItem formFlex">
						<view class="label">收货人姓名</view>
						<view class="controls">
							<view class="picker">
								<input class="input" name="consignee" value="" placeholder='请输入收货人姓名'
									placeholder-class="placeholderColor"></input>
							</view>
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">手机号码</view>
						<view class="controls">
							<view class="picker">
								<input type="number" class="input" name="mobile" value="" placeholder='请输入手机号码'
									placeholder-class="placeholderColor" maxlength="11"></input>
							</view>
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">选择地区</view>
						<view class="controls">
							<view class="picker" @click="regionChange">
								<text>{{province}}-{{city}}-{{area}}</text>
							</view>
							<u-picker mode="region" v-model="regionShow" :area-code='["11", "1101", "110101"]'
								@confirm="regionConfirm"></u-picker>
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">详细地址</view>
						<view class="controls">
							<view class="picker">
								<input class="input" name="address" value="" placeholder='请输入详细地址'
									placeholder-class="placeholderColor"></input>
							</view>
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">设为默认</view>
						<view class="controls">
							<view class="picker">
								<switch :checked="switchChecked" color="#2CAB68" @change="switchChange" />
							</view>
						</view>
					</view>
				</view>

				<!-- 底部 -->
				<view class="formPost_con">
					<button form-type="submit" hover-class="none" class="submitBtn">新 增</button>
				</view>
			</form>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 省市区
				province: '请选择',
				city: '请选择',
				area: '请选择',
				regionShow: false,

				// 设为默认
				switchChecked: true,
			}
		},
		onLoad(option) {
			// uni.showLoading({
			// 	title: '加载中'
			// });
		},
		onShow() {},
		onReady() {

		},
		methods: {
			/**
			 * 选择省市区
			 */
			regionChange() {
				this.regionShow = true;
			},
			regionConfirm(e) {
				console.log(e);
				this.province = e.province.label;
				this.city = e.city.label;
				this.area = e.area.label;
			},

			// 设为默认
			switchChange(e) {
				console.log('设为默认', e.target.value);
				this.switchChecked = e.target.value
			},

			// 新增地址
			formSubmit: function(e) {
				let _this = this;
				var formData = e.detail.value;

				if (!formData.consignee) {
					uni.showModal({
						content: '收货人姓名不能为空',
						showCancel: false,
					})
					return;
				}

				if (!formData.mobile) {
					uni.showModal({
						content: '手机号码不能为空',
						showCancel: false,
					})
					return;
				} else if (_this.$u.test.mobile(formData.mobile) == false) {
					uni.showModal({
						content: '请输入正确的手机号码',
						showCancel: false,
					})
					return;
				}

				if (_this.province == '请选择') {
					uni.showModal({
						content: '请选择地区',
						showCancel: false,
					})
					return;
				}

				if (!formData.address) {
					uni.showModal({
						content: '地址不能为空',
						showCancel: false,
					})
					return;
				}

				_this.$u.post('/api/user.address/address_add_do', {
					consignee: formData.consignee,
					mobile: formData.mobile,
					province: _this.province,
					city: _this.city,
					country: _this.area,
					address: formData.address,
					is_default: _this.switchChecked ? '1' : '0',
				}).then(res => {
					console.log("==新增地址==");
					console.log(res);

					uni.showToast({
						title: res.msg,
						duration: 2000
					});
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				}).catch(res => {
					console.log(res);
				})
			},
		}
	}
</script>

<style scoped>
	/* 表单css修正 */
	.formItem .controls {
		text-align: left;
	}

	.formItem .input {
		height: 40rpx;
		line-height: 40rpx;
		font-size: 28rpx;
		background-color: #fff;
		padding: 0;
	}

	/* 底部 */
	.formPost_con {
		width: 100%;
		padding: 0;
		position: fixed;
		bottom: 0;
	}

	.submitBtn {
		border-radius: 0;
	}
</style>
